package com.sdgakj.web.h5;

import com.google.common.collect.Maps;
import com.google.common.collect.ObjectArrays;
import com.sdgakj.entity.Feed;
import com.sdgakj.entity.News;
import com.sdgakj.entity.Message;
import com.sdgakj.entity.User;
import com.sdgakj.enums.EnumFeedType;
import com.sdgakj.rest.UserLoginRestController;
import com.sdgakj.service.feed.FeedService;
import com.sdgakj.service.msg.MessageService;
import com.sdgakj.service.msg.MessageUserService;
import com.sdgakj.service.user.UserService;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.asn1.dvcs.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.web.Servlets;

import javax.servlet.ServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.uid;


@Controller
@RequestMapping(value = "/h5/index")
public class H5IndexController {

	private static final String PAGE_SIZE = "10";
	@Autowired
	private MessageService messageService;

	@Autowired
	private MessageUserService messageUserService;

	@Autowired
	private FeedService feedService;
	@Autowired
	private UserService userService;
	private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
	private static Logger logger = LoggerFactory.getLogger(UserLoginRestController.class);

	static {
		sortTypes.put("auto", "自动");
	}

	/**
	 * 更新管理员用户信息
	 *
	 * @return
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String homeJsp() {
		return "h5/index/index";
	}

	/**参考使用
	 * 列表
	 *
	 * @param pageNumber 页号
	 * @param pageSize   每页大小
	 * @param sortType   排序字段
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
					   @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
					   @RequestParam(value = "sortType", defaultValue = "auto") String sortType, Model model,
					   ServletRequest request) {
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
//		searchParams.put("EQ_deltag",EnumIsDelete.NOTDELETE.getCode());
//		searchParams.put("EQ_type", EnumMsgType.OTHER.getCode());
		Object gtdate = searchParams.get("GT_createDate");
		Object ltdate = searchParams.get("LTE_createDate");
		if (searchParams.get("GT_createDate") != null && StringUtils.isNotEmpty(searchParams.get("GT_createDate").toString())) {
			searchParams.put("GT_createDate", com.sdgakj.utils.DateUtil.formatStringToDate(searchParams.get("GT_createDate") + " 00:00:00", "yyyy-MM-dd HH:mm:ss"));
		}
		if (searchParams.get("LTE_createDate") != null && StringUtils.isNotEmpty(searchParams.get("LTE_createDate").toString())) {
			searchParams.put("LTE_createDate", com.sdgakj.utils.DateUtil.formatStringToDate(searchParams.get("LTE_createDate") + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
		}
		String value = "";

		Page<Message> msgs = messageService.getMessage(searchParams, pageNumber, pageSize, "createDate", sortType);
		model.addAttribute("msgs", msgs);
		model.addAttribute("sortType", sortType);
		searchParams.put("LIKE_opname", value);
		if (gtdate != null) {
			searchParams.put("GT_createdate", gtdate);
		}
		if (ltdate != null) {
			searchParams.put("LTE_createdate", ltdate);
		}
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		return "msg/msgList";
	}

	/**
	 * 跳转添加页面
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String create(Model model) {
		model.addAttribute("action", "create");
		return "msg/msgForm";
	}

	/**
	 * 跳转全体发布页面
	 *
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "createall", method = RequestMethod.GET)
	public String createall(Model model) {
		model.addAttribute("action", "acreate");
		return "msg/msgAllForm";
	}
	/**
	 * 6.22从写
	 *  通过用户查找到用户的家族，找到家族通过feed表中的家族id判断是否是这个家族的消息，
	 *	倒序 跳转到index/index.jsp
	 * */
	@RequestMapping(value = {"/home"},method = {RequestMethod.GET})
	public String news(@RequestParam(value = "userId",required = false) Long userId, Model model) {
		List<Feed> today=new ArrayList<>();//今天
		List<Feed> yesterday=new ArrayList<>();//不是今天，之前的
		List<Feed> list=new ArrayList<>();
		//所有人的消息集合
		list=feedService.findFeedAll();
		for(Feed feed:list){
			//获取当前日期前一天
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE,-1);
			String stryester = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
			//获取贴子发表日期
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			String strnow=sdf.format(feed.getCreateDate());
			//转成date类型
			try {
				//今天
				Date now=sdf.parse(strnow);
				//昨天
				Date yester=sdf.parse(stryester);
				int day=yester.compareTo(now);
				if(day<0 ){
					today.add(feed);
				}
				if(day>0 || day==0){
					yesterday.add(feed);
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}

		}
		model.addAttribute("today",today);
		model.addAttribute("yesterday",yesterday);
		model.addAttribute("mess",1);
		return "h5/index/index";
	}


	/**
	 * 搜索
	 * userId
	 * @return
	 */
	@RequestMapping(value="/seache",method=RequestMethod.GET)
	public String search(@RequestParam(value = "Id", required = false) Long Id,
						 @RequestParam(value = "tit", required = false) String tit,
						 Model model){
		List<Feed> getFeedlist=feedService.getFeedlist(tit);
		Map<String, Object> searchParams =new HashMap<>();
		searchParams.put("EQ_id", "1");
		model.addAttribute("getFeedlist", getFeedlist);
		return "h5/html/seacher";
	}



	/**
	 * 分享列表详情页
	 * @return
	 */
	@RequestMapping(value="/msgDetail",method=RequestMethod.GET)
	public String share(@RequestParam(value = "id", required = false) Long id,Model model){
		Map<String, Object> searchParams =new HashMap<>();
		searchParams.put("EQ_id", "1");
		Message message = messageService.getMessage(id);
		model.addAttribute("message",message);
		return "h5/index/msgDetail";
	}

}